package com.aijia.custom.product.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aijia.custom.product.R;
import com.aijia.custom.product.customview.CustomDialog;
import com.aijia.custom.product.customview.NoDoubleClickListener;
import com.aijia.custom.product.ui.AddOrderActivity;
import com.aijia.custom.product.ui.LoginActivity;
import com.aijia.custom.product.ui.ModifyPasswordActivity;
import com.aijia.custom.product.ui.PartInActivity;
import com.aijia.custom.product.utils.SharedPreferencesUtils;

/**
 * Created by yejianjun on 2019/1/14.
 * 我的
 */

public class MyFragment extends LazyLoadBaseFragment {

    private RelativeLayout loginOutRL, modifyPassRL, partInRL;
    private TextView departmentTV;
    private TextView nameTV;
    private TextView versionTV;
    private RelativeLayout sendOrderRL;

    public static MyFragment instance() {
        MyFragment view = new MyFragment();
        //在该Fragment的构造函数中注册mTouchListener的回调
        return view;
    }


    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView(View view) {
        departmentTV = view.findViewById(R.id.department_tv);
        loginOutRL = view.findViewById(R.id.login_out_ralative);
        modifyPassRL = view.findViewById(R.id.modify_password_ralative);
        sendOrderRL = view.findViewById(R.id.add_order_relative);
        versionTV = view.findViewById(R.id.version_tv);
        nameTV = view.findViewById(R.id.my_userName_tv);
        partInRL = view.findViewById(R.id.my_partIn_rl);
        modifyPassRL.setOnClickListener(noDoubleClickListener);
        loginOutRL.setOnClickListener(noDoubleClickListener);
        sendOrderRL.setOnClickListener(noDoubleClickListener);
        partInRL.setOnClickListener(noDoubleClickListener);

        departmentTV.setText(SharedPreferencesUtils.getOrganizeName(getContext()));
        nameTV.setText(SharedPreferencesUtils.getUserName(getContext()));
        versionTV.setText(getVersionName());
    }

    private void showLoginOut() {

        CustomDialog.Builder builder = new CustomDialog.Builder(getContext());
        builder.setTitle("确定退出吗");
        builder.setPositiveButton(getContext().getResources().getString(R.string.confirm),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        startActivity(new Intent(getContext(), LoginActivity.class));
                        getActivity().finish();
                    }
                });
        builder.setNegativeButton(getContext().getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();

    }

    public String getVersionName() {
        String versionName = "";
        try {
            System.out.println("name: " + getContext().getPackageName());
            versionName = getContext().getApplicationContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.modify_password_ralative:
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ModifyPasswordActivity.class);
                    startActivity(intent);
                    break;
                case R.id.my_partIn_rl:
                    Intent partIn = new Intent();
                    partIn.setClass(getContext(), PartInActivity.class);
                    startActivity(partIn);
                    break;
                case R.id.login_out_ralative:
                    showLoginOut();
                    break;
                case R.id.add_order_relative:
                    Intent addOrderIntent = new Intent();
                    addOrderIntent.setClass(getContext(), AddOrderActivity.class);
                    startActivity(addOrderIntent);
                    break;
                default:
                    break;
            }
        }
    };

}
